﻿// <copyright file="GoogleAnalyticsExtension.cs" company="Clever Software">
// Copyright (c) 2013, 2014 All Right Reserved, http://www.cleversoftware.by
//
// This source is subject to the Microsoft Permissive License.
// Please see the License.txt file for more information.
// All other rights reserved.
//
// THIS CODE AND INFORMATION ARE PROVIDED "AS IS" WITHOUT WARRANTY OF ANY 
// KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
// IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A
// PARTICULAR PURPOSE.
//
// </copyright>
// <author>Clever Software</author>
// <email>info@cleversoftware.by</email>
// <date>2014-02-04</date> 
// <summary>CleverCMS source code</summary>

namespace CleverCMS.Core.Web.Extensions
{
    using System.Text;
    using System.Web;
    using System.Web.Mvc;

    public static partial class HtmlHelpers
    {
        public static HtmlString GoogleAnalyticsCounter(this HtmlHelper htmlHelper, string accountId)
        {
            var sb = new StringBuilder();
            sb.AppendLine("<!-- Google Analytics counter -->");
            sb.AppendLine("<script type='text/javascript'>");
            sb.AppendLine("  var _gaq = _gaq || [];");
            sb.AppendFormat("  _gaq.push(['_setAccount', '{0}']);\n", accountId);
            sb.AppendLine("  _gaq.push(['_trackPageview']);");
            sb.AppendLine("    (function() {");
            sb.AppendLine("    var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;");
            sb.AppendLine("    ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';");
            sb.AppendLine("    var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);");
            sb.AppendLine("    })();");
            sb.AppendLine("</script>");
            sb.AppendLine("<!-- /Google Analytics counter -->");
            return new HtmlString(sb.ToString());
        }
    }
}